package ecnu.ecgbackend.utils;

import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class MD5Calculator {

    public static String calculateMD5(String filePath) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            try (FileInputStream fis = new FileInputStream(filePath);
                 DigestInputStream dis = new DigestInputStream(fis, md)) {

                byte[] buffer = new byte[8192];
                while (dis.read(buffer) != -1) {
                    // 通过 DigestInputStream 读取文件内容并计算 MD5
                }
            }

            byte[] mdBytes = md.digest();
            StringBuilder result = new StringBuilder();
            for (byte mdByte : mdBytes) {
                result.append(String.format("%02x", mdByte & 0xFF));
            }

            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
